package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.CardService;
import com.cyz.admin.service.PathService;
import com.cyz.common.bean.PathCard;
import com.cyz.admin.service.PathCardService;
import com.cyz.common.dto.PathCardDto;
import com.cyz.common.mapper.PathCardMapper;
import com.cyz.common.utils.R;
import com.cyz.admin.vo.PathCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author 34928
* @description 针对表【path_card】的数据库操作Service实现
* @createDate 2023-02-28 21:03:28
*/
@Service
public class PathCardServiceImpl extends ServiceImpl<PathCardMapper, PathCard>
    implements PathCardService{

    @Autowired
    private PathService pathService;
    @Autowired
    private CardService cardService;

    @Override
    public R queryCardsAndPath() {
        List<PathCardVo> pathCardVos = new ArrayList<>();
        //查询数据
        List<PathCard> list = list();
        //根据id添加值
        list.forEach(item -> {
            PathCardVo pathCardVo = new PathCardVo();
            pathCardVo.setId(item.getId());
            pathCardVo.setPathName(pathService.getById(item.getPathId()).getName());
            pathCardVo.setCardName(cardService.getById(item.getCardId()).getTitle());
            pathCardVo.setLevel(item.getLevel());
            pathCardVos.add(pathCardVo);
        });

        return R.success(pathCardVos);
    }

    @Override
    public R addPathCard(PathCardDto pathCardDto) {
        save(new PathCard(pathCardDto.getPathId(),pathCardDto.getCardId(),pathCardDto.getLevel()));
        return queryCardsAndPath();
    }

    @Override
    public R updatePathCard(PathCardDto pathCardDto) {

        updateById(new PathCard(pathCardDto.getId(),pathCardDto.getPathId(),pathCardDto.getCardId(),pathCardDto.getLevel()));
        return queryCardsAndPath();
    }

    @Override
    public R deletePathCards(Integer[] ids) {
        removeByIds(Arrays.asList(ids));
        return queryCardsAndPath();
    }
}




